package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.huawei.chengdu.meetingdemo.R;

public class SiteCallSettingDialog extends Dialog {
    public static int encryptMode = 2; //默认自动加密
    public static char[] confPassword = new char[]{};
    public static int bandWidth = 3840; // 默认4M
    private static final String TAG = SiteCallSettingDialog.class.getSimpleName();

    private EditText siteCallPassword;
    private ImageView dialogExit;
    private TextView siteCallBandwidth;
    private RadioGroup siteCallEncryptMode;
    private RadioButton encryptAuto;
    private RadioButton encryptForce;
    private RadioButton checkedBandwidth;
    private ImageView siteCallBandWidthSelect;
    private Button siteCallSettingConfirm;
    private Context mContext;
    private BandwidthSelectDialog bandwidthSelectDialog;

    public SiteCallSettingDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        setContentView(R.layout.dialog_site_call_setting);
        initView();
        initListener();
    }

    private void initView() {
        siteCallPassword = findViewById(R.id.site_call_conf_password);
        siteCallBandwidth = findViewById(R.id.site_call_bandwidth);
        dialogExit = findViewById(R.id.site_call_exit);
        siteCallSettingConfirm = findViewById(R.id.site_call_setting_confirm);
        siteCallEncryptMode = findViewById(R.id.site_call_encrypt_mode);
        encryptAuto = findViewById(R.id.site_call_encrypt_auto);
        encryptForce = findViewById(R.id.site_call_encrypt_force);
        siteCallBandWidthSelect = findViewById(R.id.site_call_bandwidth_select);
        bandwidthSelectDialog = new BandwidthSelectDialog(mContext);
    }

    private void initData() {
        siteCallPassword.setText(String.copyValueOf(confPassword));
        siteCallBandwidth.setText(BandwidthSelectDialog.bandwidthMap.get(bandWidth));
        bandwidthSelectDialog.setCheckedBandwidth(bandWidth);
        if (Integer.parseInt(encryptAuto.getTag().toString()) == encryptMode) {
            encryptAuto.setChecked(true);
        } else {
            encryptForce.setChecked(true);
        }
    }

    private void initListener() {
        dialogExit.setOnClickListener(v -> dismiss());
        bandwidthSelectDialog.bandwidthConfirm.setOnClickListener(v -> handleBandwidthConfirm());
        siteCallBandWidthSelect.setOnClickListener(v -> bandwidthSelectDialog.show());
        siteCallSettingConfirm.setOnClickListener(v -> handleSiteCallSettingConfirm());
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        initData();
        super.show();
    }

    private void handleBandwidthConfirm() {
        checkedBandwidth = bandwidthSelectDialog.findViewById(bandwidthSelectDialog.bandwidthSelect.getCheckedRadioButtonId());
        siteCallBandwidth.setText(checkedBandwidth.getText());
        bandwidthSelectDialog.dismiss();
    }

    private void handleSiteCallSettingConfirm() {
        checkedBandwidth = checkedBandwidth != null ? checkedBandwidth :
                bandwidthSelectDialog.findViewById(bandwidthSelectDialog.bandwidthSelect.getCheckedRadioButtonId());        encryptMode = Integer.parseInt(findViewById(siteCallEncryptMode.getCheckedRadioButtonId()).getTag().toString());
        confPassword = siteCallPassword.getText().toString().toCharArray();
        if (confPassword.length != 6) {
            Toast.makeText(mContext, "请输入6位长度密码", Toast.LENGTH_LONG).show();
            return;
        }
        bandWidth = Integer.parseInt(checkedBandwidth.getTag().toString());
        dismiss();
    }

}
